package com.advertisementsystem.Mapper;

import com.advertisementsystem.entity.Role;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RoleMapper extends BaseMapper<Role> {
    @Select("SELECT r.* FROM role r " +
            "LEFT JOIN user_role ur ON r.id = ur.role_id " +
            "WHERE ur.user_id = #{userId} " +
            "AND r.status = 1")
    List<Role> selectRolesByUserId(Long userId);

    @Select({
            "<script>",
            "SELECT * FROM role",
            "<where>",
            "  <if test='name != null and name != \"\"'>",
            "    AND name LIKE CONCAT('%', #{name}, '%')",
            "  </if>",
            "  <if test='code != null and code != \"\"'>",
            "    AND code LIKE CONCAT('%', #{code}, '%')",
            "  </if>",
            "  <if test='status != null'>",
            "    AND status = #{status}",
            "  </if>",
            "</where>",
            "ORDER BY id DESC",
            "</script>"
    })
    Page<Role> selectRolesByCondition(Page<Role> page, @Param("name") String name,
                                      @Param("code") String code, @Param("status") Integer status);
}